<?php
/***
 * 描述：客户订单页面
 * @category		Mobile		
 * @package			Action			
 * @subpackage		
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-4-6
 * @version			$Id: CustOrderAction.class.php 53 2013-11-19 08:00:44Z whtaok@gmail.com $
 */
class CustOrderAction extends AuthWwwAction {
	/**
	 * 描述：订单列表列表
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function indexAct() {
		//order_status:1:处理中  2：配货中  3：配送中  4：处理完毕   //操作员审核
		//cust_order_status:1:未退货 2：全部退货  3：部分退货	//客户操作
		//is_show:0:隐藏订单   1：显示订单  //客户操作
		//is_valid:1:新订单 2：有效单 3：异常单  4：无效单  //由管理员进行财务审核（设置无效的时候，要将单子隐藏掉）
		$search['is_history'] = I('is_history');
		if($search['is_history']){
			$this->setPageTitle('历史订单');
		}else{
			$this->setPageTitle('进行中的订单');
		}
		list($list,$page) = OrderModel::findOrderListPage($this->curr_cust_id, $search);
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->display('Cust/Order/index');
	}
	
	public function showDetailAct(){
		
		$orderCode = I('order_code');
		$this->setPageTitle("订单详细-$orderCode");
		list($order,$items) = OrderModel::findOrderDetail($this->curr_cust_id, $orderCode);
		$this->assign('orderDetail',$order);
		$this->assign('orderItems',$items);
		$this->display('Cust/Order/detail');
	}
	
	/**
	 * 描述：订单确认页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */	
	public function showConfirmAct(){
		$this->setPageTitle('选择配送地址');
		//配送地址信息
		$mod = new CommonModel('CustAddress');
		$sendList = $mod->setModWhere(array('cust_id'=>session(SES_CUST_ID)))->order('is_default desc')->select();
		if($sendList){
			$address = $sendList[0];
			$address['add_flag'] = 0; //地址存在
		}else{
			$cust = session(SES_CUST);
			$address['name']=$cust['name'];
			$address['mobile']=$cust['mobile'];
			$address['address']=cookie('cust_address');
			$address['add_flag'] = 1;//地址不存在，提交订单时添加地址
		}
		//配送时间
		$sendTime = get_shop_setting($this->curr_shop_id, '配送时间');
		if(!$sendTime){
			//如果没有在数据库中设定，取配置文件
			$sendTime = C('SHOP_SEND_TIME');
		}
		//购物车信息
		$cart = CartModel::getCart(session(SES_SHOP_ID));
		$this->assign('cartItems',$cart['cartItems']);
		$this->assign('cartPrice',$cart['cartPrice']);
		$this->assign('address',$address);
		$this->assign('sendTime',$sendTime);

		$this->display('Cust/Order/confirm');
	}
	
	/**
	 * 描述：订单提交操作
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doOrderAct(){
		$shop_id = $this->curr_shop_id;
		$cust_id = $this->curr_cust_id;
		$address = array(
			'cust_id'=>$cust_id,
			'cust_name'=>I('cust_name'),
			'cust_mobile'=>I('cust_mobile'),
			'cust_address'=>I('cust_address'),
		);
		//判断是否保存地址信息到客户地址本中
		if(I('address_add_flag') == '1' ){
			$mod = new CustAddressModel();
			$address_num = $mod->setModWhere(array('cust_id'=>$cust_id))->count();
			if($address_num>0){
				//地址已存在
				cookie('cust_address',null);
			}else{
				//地址不存在
				$mod = new CustAddressModel();
				$mod->doAdd($address,1);
				cookie('cust_address',$address['cust_address']);				
			}			
		}
		//购物车商品校验
		$cart = CartModel::checkCart($shop_id);
		if($cart['error']=='1'){
			$this->assign('cartItems',$cart['cartItems']);
			$this->assign('cartPrice',$cart['cartPrice']);
			$this->display('Cart/index');
			exit();
		}
		if(count($cart['cartItems'])==0){
			$this->assign('cartItems',$cart['cartItems']);
			$this->assign('cartPrice',$cart['cartPrice']);
			$this->display('Cart/index');
			exit();
		}
		$orderItems = $cart['cartItems'];
		$orderPrice = $cart['cartPrice'];
		$orderTime = time();
		$orderCode = gen_order_code();
		$orderData = array(
			'order_code'=>$orderCode,
			'order_status'=>1,
			'order_amt'=>$orderPrice['total_amt'],
			'order_send_fee'=>$orderPrice['send_fee'],
			'cust_id'=>$address['cust_id'],
			'cust_name'=>$address['cust_name'],
			'cust_mobile'=>$address['cust_mobile'],
			'cust_address'=>$address['cust_address'],
			'cust_free_time'=>I('cust_free_time'),
			'cust_order_status'=>0,
			'cust_order_time'=>$orderTime,
			'cust_bak'=>I('cust_bak'),
			'shop_id'=>$shop_id,
			'client_type'=>1,
			'crt_time'=>$orderTime,
			'crt_user'=>$cust_id,
			'upd_time'=>$orderTime,
			'upd_user'=>$cust_id,
		);
		$orderDetailData = array();
		$orderStockData = array();
		foreach($orderItems as $key=>$value){
			$orderDetailData[] = array(
				'order_code' => $orderCode,
				'prod_id' => $value['prod_id'],
				'prod_name' => $value['name'],
				'prod_spec' => $value['spec'],
				'prod_spec_qty' => $value['spec_qty'],
				'prod_spec_unit' => $value['spec_unit'],
				'prod_price_real' => $value['price_real'],
				'prod_price_sale' => $value['price_sale'],
				'prod_num' => $value['num'],
				'prod_stock_prod_id' => $value['stock_prod_id'],
				'cust_id' => $cust_id,
				'crt_time'=>$orderTime,
				'crt_user'=>$cust_id,
				'upd_time'=>$orderTime,
				'upd_user'=>$cust_id,
			);
			$orderStockData[] = array(
				'prod_id' => $value['prod_id'],
				'stock_prod_id' => $value['stock_prod_id'],
				'num' => $value['num'],
				'spec_qty' => $value['spec_qty'],
			);
		}
		$tranFlag = OrderModel::addOrder($orderData, $orderDetailData);
		if($tranFlag){
			//处理库存，处理失败不影响后续操作，但是生成一条错误记录提醒后台
			StockModel::updOrderStock($shop_id,$orderCode, $orderStockData);
			cookie(COK_CART,null);
			$this->redirect('CustOrder/showOrderSuccess',array('order_code'=>$orderCode));
		}else{
			$this->error('订单提交失败，请重新尝试');
		}

	}
	/**
	 * 描述：订单提交成功页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showOrderSuccessAct(){
		$this->setPageTitle('等待收货');
		$orderCode = I('order_code');
		$this->assign('order_code',$orderCode);
		$this->display('Cust/Order/result');
	}
}

?>